package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;

import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {


    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */

    @Override
    public void save(SetmealDTO setmealDTO) {
        //拷贝setmeal 插入
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setMealMapper.insert(setmeal);
        //拷贝setmealdish 插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //获取id
        Long setmealId = setmeal.getId();
        for (SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmealId);
            setMealDishMapper.insert(setmealDish);
        }

    }
    /**
     * 套餐分页查询
     */
    @Override
    public Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setMealMapper.pageQuery(setmealPageQueryDTO);

        return page;
    }

    /**
     * 套餐设置起售禁售
     * @param status
     */
    @Override
    public void setStartOrCloce(Integer status,Integer id) {
        setMealMapper.setStartOrCloce(status,id);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO=setMealMapper.getById(id);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //跟新setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        //更新setmeal_dish
           //删除套餐分类

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long id = setmeal.getId();
        //删除套餐分类
        setMealDishMapper.deleteBySetmealId(id);
        for(SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(id);
            setMealDishMapper.insert(setmealDish);
        }


    }

    @Override
    public void deleteByIds(List<Long> ids) {
        // setmeal删除套餐
        setMealMapper.deleteByIds(ids);
        //setmeal_dish 根据ids 删除
        setMealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }

}
